import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import lay from '@/components/lay/lay.vue'
import index from '@/views/index.vue'
import Login from '@/views/login/login.vue'
import { useTokenStore } from '@/stores/mytoken' // 顶部引入




const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
     {
          path: '/login',
          name: 'login',
          // 路由懒加载
          // 当访问/login时，才加载login.vue组件
          component: () => import('../views/login/login.vue'),
        },
    {
      path: '/',
      name: 'home',
      component: lay,
      meta: { requireAuth: true,title: '首页' }, // 添加元信息，表示需要登录才能访问
      children:[
        {
          path: '',
          name: 'homeIndex', // 新增name
          component: index,
        },
         {
          path: '/menus',
          name: 'menus',
           meta: { requireAuth: true },
          component: () => import('@/views/menu/MenuIndex.vue'),
          meta:{title:'菜单列表'},
        },
        {
          path: '/menus/create',
          name: 'menusCreate',
          component: () => import('@/views/menu/CreateOrEdit.vue'),
          meta:{title:'新增菜单'},
        },
        {
          path: '/menus/:id/edit',
          name: "menu-edit",
          component: () => import('@/views/menu/CreateOrEdit.vue'),
          meta:{title:'编辑菜单'},
        },
          {
            path : '/resource-category',
            name : 'resource-category',
            component: () => import('@/views/resource-category/ResourceCategoryIndex.vue'),
            meta:{title:'资源分类'},
          },
          {
            path: "/resources",
            name: "resources",
            component: () =>import("@/views/resources/ResourcesIndex.vue"),
            meta:{title:'资源列表'},
          },
          {
            path: "/roles",
            name: "roles",
            component: () =>import("@/views/roles/RolesIndex.vue"),
            meta:{title:'角色列表'},
          },
           {
            path: "/roles/:roleId/alloc-menus",
            name: "alloc-menus",
            component: () =>import("@/views/roles/AllocMenus.vue"),
            props:true,
            meta:{title:'分配菜单'},
          },
            {
            path: "/roles/:roleId/alloc-resources",
            name: "alloc-resources",
            component: () =>import("@/views/roles/AllocResources.vue"),
            props:true,
            meta:{title:'分配资源'},
          },
           {
            path: "/users",
            name: "users",
            component: () =>import("@/views/users/UsersIndex.vue"),
            meta:{title:'用户列表'},
          },
          {
            path: "/courses",
            name: "courses",
            component: () =>import("@/views/courses/CoursesIndex.vue"),
            meta:{title:'课程列表'},
          },
           {
            path: "/courses/create",
            name: "courses-create",
            component: () =>import("@/views/courses/CreateOrEdit.vue"),
            meta:{title:'新增课程'},
          },
          {
            path: "/courses/:courseId/edit",
            name: "courses-edit",
            component: () =>import("@/views/courses/CreateOrEdit.vue"),
           props: true, // 传递courseId参数
            meta:{title:'编辑课程'},
          },
      ],
    },
   
       
        {
          path: '/:xxx(.*)',
          name: 'error',
           meta: { requireAuth: true },
          component: () => import('@/views/error.vue'),
        }
    
  ],
})
//判断是否需要登录才能访问
router.beforeEach((to, from, next) => {
  const store = useTokenStore()
  const token = store.token
  if (to.matched.some(r => r.meta?.requireAuth)) {
    if (!token || !token.access_token) {
      next({ name: "login", query: { redirect: to.fullPath } });
    } else {
      next();
    }
  } else {
    next();
  }
})
export default router
